$(document).ready(function () {
    init();

    function init() {
        $.ajax({
            type: "get",
            url: urlUserCenter + "/clazz/getClazz",
            dataType: "json",
            success: function (data) {
                for (var i in data) {
                    $("#selectClazz").append("<option value=" + data[i].id + ">" + data[i].name + "</option>");
                }
            },
            error: function (err) {
                console.log(err);
            }
        })
    }

    $("#register").click(function () {
        var userName = $("#userName").val();
        var password = $("#password").val();
        var confirmPassword = $("#confirmPassword").val();
        var email = $("#email").val();
        var clazzid = $("#selectClazz").val();
        var myReg = /^[a-zA-Z0-9_-]+@([a-zA-Z0-9]+\.)+(com|cn|net|org)$/;//查看邮箱格式是否正确
        var user = {
            name: userName,
            password: password,
            email: email,
            clazzid: clazzid,
            type: "1"
        };
        if (userName == null || userName == "" || password == null || password == "" || confirmPassword == null || confirmPassword == "" ||
            email == null || email == "") {
            alert("你还有东西没有填写完毕！");
            return;
        } else if (!myReg.test(email)) {
            alert("邮箱格式不对!");
            return;
        } else if (password != confirmPassword) {
            alert("密码不一致!");
            return;
        } else  {
            $.ajax({
                type: "POST",
                url:urlUserCenter+ "/user/getAllUsers",
                dataType: "json",
                async: false,
                success:function (data) {
                    var flag = 0;
                    for (var i in data) {
                        if (userName == data[i].name) {
                            alert("用户名已存在，请重新输入！");
                            flag = 1;
                            break;
                        } else {
                            if (email == data[i].email) {
                                alert("邮箱被占用，请重新输入！");
                                flag = 1;
                                break;
                            }
                        }
                    }
                    if (flag === 0) {
                        $.ajax({
                            type: "post",
                            url: urlUserCenter + "/user/addUser",
                            dataType: "json",
                            data: user,
                            success: function (data) {
                                console.log(data);
                                if (data.result == "success") {
                                    alert("注册成功!");
                                    window.location.href = "login.html";
                                } else if (data.result == "false") {
                                    alert("注册失败!");
                                    return;
                                }
                            },
                            error: function (xhr, textStatus) {
                                console.log('错误');
                                console.log(xhr);
                                console.log(textStatus);
                            }
                        });
                    }
                },
                error:function (xhr, textStatus) {
                    console.log('错误');
                    console.log(xhr);
                    console.log(textStatus);
                }
            })

        }
    });
    $("#login").click(function () {
        window.location.href = "login.html";
    })

});